Option Strict Off
Option Explicit On
Friend Class frmNewGame
	Inherits System.Windows.Forms.Form
	Private Declare Function SetCurrentDirectory Lib "kernel32"  Alias "SetCurrentDirectoryA"(ByVal lpPathName As String) As Integer
	Public m_Response As String
	Public m_SavedGame As String
	Private Sub cancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cancel.Click 'cancel
		Me.Hide()
		Me.Close()
		m_Response = "cancel"
	End Sub
	
  Private Sub chkResume_CheckStateChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles chkResume.CheckStateChanged
    cmdLoadGame.Enabled = (chkResume.CheckState = 1)
  End Sub
	
	Private Sub cmdLoadGame_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdLoadGame.Click
		If optOffline.Checked = True Then
      MsgBox(My.Resources.MSG_NOT_ALLOWED_MODE)
			Exit Sub
		End If
		On Error GoTo er
    With cdlGameOpen
      .Filter = "Stratego saved game files (*.str)|*.str|All files (*.*)|*.*"
      If .ShowDialog() = Windows.Forms.DialogResult.OK Then
        m_SavedGame = .FileName
      End If
    End With
		Call SetCurrentDirectory(My.Application.Info.DirectoryPath)
		Exit Sub
er: 
		Call SetCurrentDirectory(My.Application.Info.DirectoryPath)
		chkResume.CheckState = System.Windows.Forms.CheckState.Unchecked
	End Sub
	
	Private Sub ok_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles ok.Click 'OK
		Me.Hide()
		m_Response = "ok"
    'On Error Resume Next
    Dim d As String
    If Not Form1.Connected AndAlso Not Form1.PlayOffline Then ' start online game
      If chkResume.CheckState = 1 And Len(m_SavedGame) = 0 Then
        MsgBox(My.Resources.MSG_SPECIFY_GAME)
        Exit Sub
      End If
      NameNIP.ShowDialog()
    Else
      If Form1.Connected Then ' we have a game in progress and we want a new one
        If Form1.Host Then
          d = "Yes"
        Else
          d = "No"
        End If
        If Form1.Host Then
          Form1.SockClient.Send("#020code_okconnect" & Form1.NickName)
        Else
          Form1.RequestGame()
        End If
        'Form1.SockClient.SendData "#020code_wantnewga" & d
        'EraseBoard
        Form1.sb1.Text = My.Resources.MSG_WAITING_ANSWER
      Else ' offline
        NameNIP.ShowDialog()
      End If ' if connected
    End If
	End Sub
	
	Private Sub frmNewGame_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
		' network game by default
		optOnline.Checked = Not Form1.PlayOffline
		optOffline.Checked = Form1.PlayOffline
		
		optArrange1.Enabled = Form1.PlayOffline
		optArrange2.Enabled = Form1.PlayOffline
    optArrange1.Checked = (ArrangeMode = 1)
    optArrange2.Checked = (ArrangeMode = 2)
		
		optHost.Enabled = Not Form1.PlayOffline
		optVisitor.Enabled = Not Form1.PlayOffline
		optHost.Checked = Form1.Host
		optVisitor.Checked = Not Form1.Host
		chkResume.Enabled = Not Form1.Host
	End Sub
	
  Private Sub optArrange1_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optArrange1.CheckedChanged
    If eventSender.Checked Then
      ArrangeMode = 1
    End If
  End Sub
	
  Private Sub optArrange2_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optArrange2.CheckedChanged
    If eventSender.Checked Then
      ArrangeMode = 2
    End If
  End Sub
	
  Private Sub optHost_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optHost.CheckedChanged
    If eventSender.Checked Then
      Form1.Host = True
      chkResume.Enabled = False
      'Form1.NickName = "Host"
    End If
  End Sub
  Private Sub optVisitor_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optVisitor.CheckedChanged
    If eventSender.Checked Then
      Form1.Host = False
      chkResume.Enabled = True
      'Form1.NickName = "Guest"
    End If
  End Sub
	
  Private Sub optOffline_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optOffline.CheckedChanged
    If eventSender.Checked Then
      Call optOptions()
    End If
  End Sub
	
  Private Sub optOnline_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optOnline.CheckedChanged
    If eventSender.Checked Then
      Call optOptions()
    End If
  End Sub
	Private Sub optOptions()
		If optOffline.Checked Then
			optHost.Enabled = False : optVisitor.Enabled = False
			optArrange1.Enabled = True : optArrange2.Enabled = True
			chkResume.Enabled = False : cmdLoadGame.Enabled = False
			Form1.Host = True : Form1.PlayOffline = True
		Else
			optHost.Enabled = True : optVisitor.Enabled = True
			optArrange1.Enabled = False : optArrange2.Enabled = False
			chkResume.Enabled = Not optHost.Checked : cmdLoadGame.Enabled = Not optHost.Checked
			Form1.PlayOffline = False
		End If
	End Sub
End Class